package com.our.sys.project1.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.service.HzService;
import com.our.sys.project1.dao.ActionTypeDao;
import com.our.sys.project1.entity.ActionType;

@Service
@Transactional
public class ActionTypeService {

    @Autowired
    private ActionTypeDao actionTypeDao;

    @Autowired
    private HzService hzService;

    private static Logger logger = LoggerFactory.getLogger(ActionTypeService.class);

    @Transactional(readOnly = true)
    public Page<ActionType> query(Page<ActionType> page, List<PropertyFilter> filters) {
        Page<ActionType> curPage = actionTypeDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public ActionType queryById(Long id) {
        return actionTypeDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    private Long queryCountByName(String name) {
        Long result = actionTypeDao.findUnique("select count(a) from ActionType a where a.name = ?", name);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    public void save(ActionType actionType) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(actionType.getName());
            actionType.setPy(map.get("py"));
            actionType.setWb(map.get("wb"));
            if (null != actionType.getId()) {
                ActionType getActionType = this.queryById(actionType.getId());

                if (queryCountByName(getActionType.getName()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个操作名称！");
                }

                getActionType.setName(actionType.getName());
                getActionType.setType(actionType.getType());
                getActionType.setPy(actionType.getPy());
                getActionType.setWb(actionType.getWb());
                getActionType.setIsOther(actionType.getIsOther());
                getActionType.setIsVisiable(actionType.getIsVisiable());
                getActionType.setState(actionType.getState());
                actionTypeDao.save(getActionType);
            } else {

                if (queryCountByName(actionType.getName()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个操作名称！");
                }
                actionTypeDao.save(actionType);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void delete(Long id) throws Exception {
        try {
            actionTypeDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
}
